package com.company.cloud.company.modules.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.company.modules.api.response.ThirdPlatformConfigModel;
import com.company.cloud.company.modules.app.controller.req.ThirdPlatformConfigSave2EditParam;
import com.company.cloud.company.modules.app.service.CompanyAppService;
import com.company.cloud.company.modules.app.service.ThirdPlatformConfigAppService;
import com.company.cloud.company.modules.base.entity.ThirdPlatformConfig;
import com.company.cloud.company.modules.base.service.ThirdPlatformConfigService;
import com.company.cloud.company.modules.cache.utils.CompanyConvertUtils;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ThirdPlatformConfigAppServiceImpl implements ThirdPlatformConfigAppService {

    @Autowired
    private ThirdPlatformConfigService thirdPlatformConfigService;
    @Autowired
    private CompanyAppService companyAppService;


    @Override
    public void save2Edit(ThirdPlatformConfigSave2EditParam param, LoginSimpleUserInfo userInfo) {
        ValidatorUtils.validate(param);
        companyAppService.checkCompanyermission(param.getCompanyNo(),userInfo.getId());

        ThirdPlatformConfig entity=thirdPlatformConfigService.getOne(new QueryWrapper<ThirdPlatformConfig>()
                .eq("company_no",param.getCompanyNo())
                .eq("third_platform_type",param.getThirdPlatformType())
        );
        if(entity==null){
            entity=new ThirdPlatformConfig();
            entity.setCompanyNo(param.getCompanyNo());
            entity.setThirdPlatformType(param.getThirdPlatformType());
            entity.setThirdPlatformConfig(JSON.toJSONString(param.getThirdPlatformConfig()));
            entity.addCreateParam(userInfo);
            entity.addEditParam(userInfo);
            thirdPlatformConfigService.save(entity);
        }else {
            entity.setThirdPlatformConfig(JSON.toJSONString(param.getThirdPlatformConfig()));
            entity.setRowsState(BaseConstant.EnabledEnum.ENABLED.getCode());
            entity.addEditParam(userInfo);
            thirdPlatformConfigService.updateById(entity);
        }
    }

    @Override
    public ThirdPlatformConfigModel getThirdPlatformConfig(Integer id) {
        if(CheckUtils.isEmpty(id)){
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        ThirdPlatformConfig entity= thirdPlatformConfigService.getById(id);
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        ThirdPlatformConfigModel data= CompanyConvertUtils.getThirdPlatformConfig(entity);
        return data;
    }


    @Override
    public List<ThirdPlatformConfigModel> getThirdPlatformConfigList(String companyNo) {
        if(CheckUtils.isEmpty(companyNo)){
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        List<ThirdPlatformConfig> list = thirdPlatformConfigService.list(
                new QueryWrapper<ThirdPlatformConfig>()
                .eq("company_no",companyNo)
                .eq("rows_state",BaseConstant.EnabledEnum.ENABLED.getCode())

        );
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ThirdPlatformConfigModel> data=new ArrayList<>();
        list.forEach(item->{
            data.add( CompanyConvertUtils.getThirdPlatformConfig(item));
        });
        return data;
    }
}
